using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using net_work.Data.Entity.Sys;
using net_work.Data.Enum.Sys;

namespace net_work.Config.DataBaseConfig.Sys;

/// <summary>
/// 学生实体配置类
/// </summary>
public class StudentEntityTypeConfiguration : IEntityTypeConfiguration<Student>
{
    public void Configure(EntityTypeBuilder<Student> builder)
    {
        builder.ToTable("sys_stu");

        builder.HasKey(s => s.Id);

        builder.HasIndex(s => s.StudentNumber).IsUnique();

        // 配置字段名映射
        builder.Property(s => s.Id).HasColumnName("stu_id");
        builder.Property(s => s.StudentNumber).HasColumnName("stu_num").IsRequired().HasMaxLength(32);
        builder.Property(s => s.StudentQualification)
            .HasColumnName("stu_qual")
            .IsRequired()
            .HasMaxLength(16)
            .HasConversion(s => s.ToString(), s => Enum.Parse<StudentQualificationLevel>(s));
        builder.Property(s => s.AdmissionDate).HasColumnName("stu_admis_date").HasColumnType("date");
        builder.Property(s => s.GraduationDate).HasColumnName("stu_grad_date").HasColumnType("date");
        builder.Property(s => s.EmergencyContactName).HasColumnName("stu_emer_ctt_name").HasMaxLength(64);
        builder.Property(s => s.EmergencyContactPhone).HasColumnName("stu_emer_ctt_phone").HasMaxLength(15);
        builder.Property(s => s.ClassId).HasColumnName("class_id").IsRequired();
        builder.Property(s => s.PsnId).HasColumnName("psn_id").IsRequired();

        builder.Property(s => s.CreatedBy).HasColumnName("create_by").IsRequired().HasMaxLength(64);
        builder.Property(s => s.CreatedTime).HasColumnName("create_time").IsRequired();
        builder.Property(s => s.UpdatedBy).HasColumnName("update_by").IsRequired().HasMaxLength(64);
        builder.Property(s => s.UpdatedTime).HasColumnName("update_time").IsRequired();
        builder.Property(s => s.Remark).HasColumnName("remark").HasMaxLength(256);
        builder.Property(s => s.DelFlag).HasColumnName("del_flag").HasDefaultValue('0');
    }
}